package cn.com.jslh.boarpro.main.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.BaseFragment;
import cn.com.jslh.boarpro.base.mvp.BaseListView;
import cn.com.jslh.boarpro.main.MainDetilActivity;
import cn.com.jslh.boarpro.main.contract.DuanNaiContract;
import cn.com.jslh.boarpro.main.presenter.DuanNaiPresenterImpl;
import cn.com.jslh.boarpro.main.presenter.DuanNaiRecordPresenter;
import cn.com.jslh.boarpro.util.FragmentFlashFlag;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * 作者：I_Gisvity on 2017/10/17 09:02
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class DuanNaiRecordFragment extends BaseFragment implements DuanNaiContract.View {

    @BindView(R.id.dataRecycleview)
    RecyclerView dataRecycleview;

    private DuanNaiContract.Presenter duanNaiPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        new DuanNaiPresenterImpl(this,getContext());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.duannai_record_layout, container, false);
        unbinder = ButterKnife.bind(this, view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        dataRecycleview.setLayoutManager(linearLayoutManager);
        dataRecycleview.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        duanNaiPresenter.bindRV(dataRecycleview);
        duanNaiPresenter.start();
        return view;
    }

    // This method will be called when a SomeOtherEvent is posted
    @Subscribe
    public void handleSomethingElse(FragmentFlashFlag flag) {
//        deliveryRecordPresenter.upDataUI();
        duanNaiPresenter.start();
    }

    public static void FlashUI() {
        EventBus.getDefault().post(new FragmentFlashFlag());
    }

    @Subscribe
    public void handleUpDataAction(MainDetilActivity.Action action) {
        if (isResumed() && ((MainDetilActivity) getContext()).getViewPage().getCurrentItem() == 0) {
            duanNaiPresenter.upAction();//上传数据
        }
    }
    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void setPresenter(DuanNaiContract.Presenter presenter) {
        this.duanNaiPresenter = presenter;
    }
}
